package com.matrix.order.infrastructure.gateway.acl;

import com.matrix.api.merchant.MerchantApi;
import com.matrix.api.merchant.vo.ShopDetailVO;
import com.matrix.api.merchant.vo.ShopInfoVO;
import com.matrix.common.enums.ShopStatusEnum;
import com.matrix.common.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.stereotype.Component;

/**
 * @author 有点甜
 * @since 2025/8/2
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MerchantAcl {

    @DubboReference(check = false, timeout = 2000)
    private MerchantApi merchantApi;

    public void checkShopStatus(Long shopId) {
        try {
            ShopDetailVO shopDetails = merchantApi.getShopDetailsInternal(shopId);
            if (shopDetails == null) {
                throw new BusinessException("店铺信息获取失败或不存在");
            }
            if (shopDetails.getStatus() != ShopStatusEnum.OPEN) {
                throw new BusinessException("店铺已打烊,暂不接单");
            }
        } catch (RpcException e) {
            log.error("检查店铺状态失败, shopId: {}, RPC异常: {}", shopId, e.getMessage());
            throw new BusinessException("商家服务暂不可用,请稍后重试");
        }
    }

    public ShopDetailVO getShopDetailsById(Long shopId) {
        try {
            ShopDetailVO shopDetails = merchantApi.getShopDetailsInternal(shopId);
            if (shopDetails == null) {
                throw new BusinessException("店铺信息获取失败或不存在");
            }
            return shopDetails;
        } catch (RpcException e) {
            log.error("获取店铺详情失败, shopId: {}, RPC异常: {}", shopId, e.getMessage());
            throw new BusinessException("商家服务暂不可用,请稍后重试");
        }
    }

    public void checkTableAvailability(Long shopId, String areaSeat) {
        try {
            boolean isAvailable = merchantApi.checkTableAvailability(shopId, areaSeat);
            if (!isAvailable) {
                throw new BusinessException("当前桌位已被占用,请重新选择");
            }
        } catch (RpcException e) {
            log.error("检查桌位可用性失败, shopId: {}, areaSeat: {}, RPC异常: {}", shopId, areaSeat, e.getMessage());
            throw new BusinessException("商家服务暂不可用,请稍后重试");
        }
    }

    public void occupyTable(Long shopId, String areaSeat, Long orderId) {
        try {
            merchantApi.occupyTable(shopId, areaSeat, orderId);
        } catch (RpcException e) {
            log.error("占用桌位失败, orderId: {}, RPC异常: {}", orderId, e.getMessage());
        }
    }

    public void markTableForCleanUp(Long shopId, String areaSeat) {
        try {
            merchantApi.markTableForCleanUp(shopId, areaSeat);
        } catch (RpcException e) {
            log.error("标记桌位为待清理状态失败, shopId: {}, areaSeat: {}, RPC异常: {}", shopId, areaSeat, e.getMessage());
        }
    }

    public Long getShopIdByOwnerId(Long ownerId) {
        try {
            ShopInfoVO shopInfo = merchantApi.getShopInfoByUserId(ownerId);
            if (shopInfo == null) {
                throw new BusinessException("您还没有创建店铺,请先完成入驻");
            }
            return shopInfo.getShopId();
        } catch (RpcException e) {
            log.error("获取商家ShopId失败, ownerId: {}, RPC异常: {}", ownerId, e.getMessage());
            throw new BusinessException("商家服务暂不可用,请稍后重试");
        }
    }

    public Long getOwnerIdByShopId(Long shopId) {
        try {
            ShopDetailVO shopDetails = merchantApi.getShopDetailsInternal(shopId);
            if (shopDetails == null) {
                throw new BusinessException("无法获取店铺信息, shopId: " + shopId);
            }
            return shopDetails.getOwnerId();
        } catch (RpcException e) {
            log.error("获取商家OwnerId失败, shopId: {}, RPC异常: {}", shopId, e.getMessage());
            throw new BusinessException("商家服务暂不可用,请稍后重试");
        }
    }
}